////////////////////////////////////////////////////////////////////////////////
// Filename: refraction.fx
////////////////////////////////////////////////////////////////////////////////


/////////////
// GLOBALS //
/////////////
matrix World;
matrix View;
matrix Projection;
Texture2D ShaderTexture;
float4 AmbientColor;
float4 DiffuseColor;
float3 LightDirection;
float4 ClipPlane;


///////////////////
// SAMPLE STATES //
///////////////////
SamplerState SampleType
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
};


//////////////
// TYPEDEFS //
//////////////
struct VS_IN
{
    float4 position : POSITION;
    float2 tex : TEXCOORD0;
	float3 normal : NORMAL;
};

struct VS_OUT
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
   	float3 normal : NORMAL;
   	float clip : SV_ClipDistance0;
};


////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
VS_OUT RefractionVertexShader(VS_IN input)
{
    VS_OUT output = (VS_OUT)0;
    
	// Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(input.position, World);
    output.position = mul(output.position, View);
    output.position = mul(output.position, Projection);
    
	output.tex = input.tex;
    
	// Calculate the normal vector against the world matrix only.
	output.normal = mul(input.normal, (float3x3)World);
	
	// Normalize the normal vector.
	output.normal = normalize(output.normal);

	// Set the clipping plane.
    output.clip = dot(mul(input.position, World), ClipPlane);

	return output;
}



////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 RefractionPixelShader(VS_OUT input) : SV_Target
{
	float4 textureColor;
	float3 lightDir;
	float lightIntensity;
	float4 color;
	

	// Sample the texture pixel at this location.
	textureColor = ShaderTexture.Sample(SampleType, input.tex);

	color = textureColor;

	// Set the default output color to the ambient light value for all pixels.
	color = AmbientColor;
	
	// Invert the light direction for calculations.
	lightDir = -LightDirection;

	// Calculate the amount of light on this pixel.
	lightIntensity = saturate(dot(input.normal, lightDir));
	
	if(lightIntensity > 0.0f)
	{
		// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
		color += (DiffuseColor * lightIntensity);
	}

	// Saturate the final light color.
	color = saturate(color);
	
	// Multiply the texture pixel and the input color to get the final result.
	color = color * textureColor;
	
	
	return color;
}


////////////////////////////////////////////////////////////////////////////////
// Technique
////////////////////////////////////////////////////////////////////////////////
technique10 RefractionTechnique
{
    pass pass0
    {
        SetVertexShader(CompileShader(vs_4_0, RefractionVertexShader()));
        SetPixelShader(CompileShader(ps_4_0, RefractionPixelShader()));
        SetGeometryShader(NULL);
    }
}